/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.adapter.datasource.sqldb.service.impl;

import cn.hutool.core.util.ObjectUtil;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cetccloud.cimp.adapter.datasource.sqldb.dao.ModelJoinConfigDao;
import com.cetccloud.cimp.adapter.datasource.sqldb.entity.ModelJoinConfigEntity;
import com.cetccloud.cimp.adapter.datasource.sqldb.service.ModelJoinConfigService;
import com.cetccloud.cimp.adapter.datasource.sqldb.vo.ServerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @apiNote 模型接入配置服务
 * @date 2022-12-28 17:19:48
 */
@Service("modelJoinConfigService")
public class ModelJoinConfigServiceImpl extends ServiceImpl<ModelJoinConfigDao, ModelJoinConfigEntity> implements ModelJoinConfigService {


    /**
     * 处理服务配置集
     */
    @Override
    public List<ServerConfig> selectAllConfig(String serverId){
        List<ServerConfig>  configList = this.baseMapper.selectAllConfig(serverId);
        if(ObjectUtil.isNotEmpty(configList)) {
            Map<Integer, List<String>> groupData = configList.stream()
                    .collect(Collectors.groupingBy(ServerConfig::getId, Collectors.mapping(ServerConfig::getDoLink, Collectors.toList())));

            Map<Integer, List<String>> groupLink = configList.stream()
                    .collect(Collectors.groupingBy(ServerConfig::getId, Collectors.mapping(ServerConfig::getLinks, Collectors.toList())));

            Map<Integer, List<ServerConfig>> groupConfig = configList.stream()
                    .collect(Collectors.groupingBy(ServerConfig::getId));

            List<ServerConfig> configGroup = new ArrayList<>();
            groupConfig.entrySet().forEach(server -> {
                ServerConfig serverConfig = server.getValue().get(0);
                serverConfig.setDoLinkList(groupData.get(server.getKey()));
                serverConfig.setLinksList(groupLink.get(server.getKey()));
                configGroup.add(serverConfig);
            });

            return configGroup;
        }
        return null;
    }

    /**
     * 获取设备信息
     */
    @Override
    public Map<String,String> getBindByDtuId(String dtuId){
        return this.baseMapper.getBindByDtuId(dtuId);
    }
}